package com.gxg.newdemo1.mynewdamo1.channel.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import android.database.SQLException;
import android.util.Log;

import com.gxg.newdemo1.mynewdamo1.channel.dao.ChannelDao;
import com.gxg.newdemo1.mynewdamo1.channel.db.SQLHelper;
import com.socks.library.KLog;


public class ChannelManage {
    public static ChannelManage channelManage;
    /**
     * 默认的用户选择频道列表
     */
    public static List<ChannelItem> defaultUserChannels;
    /**
     * 默认的其他频道列表
     */
    public static List<ChannelItem> defaultOtherChannels;

    private ChannelDao channelDao;
    /**
     * 判断数据库中是否存在用户数据
     */
    private boolean userExist = false;


    private ChannelManage(SQLHelper paramDBHelper) throws SQLException {
        if (channelDao == null)
            channelDao = new ChannelDao(paramDBHelper.getContext());
        // NavigateItemDao(paramDBHelper.getDao(NavigateItem.class));
        return;
    }

    /**
     * 初始化频道管理类
     *
     * @throws SQLException
     */
    public static ChannelManage getManage(SQLHelper dbHelper) throws SQLException {
        if (channelManage == null)
            channelManage = new ChannelManage(dbHelper);
        return channelManage;
    }

    /**
     * 清除所有的频道
     */
    public void deleteAllChannel() {
        channelDao.clearFeedTable();
    }

    /**
     * 获取其他的频道
     *
     * @return 数据库存在用户配置 ? 数据库内的用户选择频道 : 默认用户选择频道 ;
     */
    public List<ChannelItem> getUserChannel() {
        Object cacheList = channelDao.listCache(SQLHelper.SELECTED + "= ?", new String[]{"1"});
        KLog.e("sss  "+cacheList.toString());
        if (cacheList != null && !((List) cacheList).isEmpty()) {
            userExist = true;
            List<Map<String, String>> maplist = (List) cacheList;
            int count = maplist.size();
            List<ChannelItem> list = new ArrayList<ChannelItem>();
            for (int i = 0; i < count; i++) {
                ChannelItem navigate = new ChannelItem();
                navigate.setRid(maplist.get(i).get(SQLHelper.RID));
                navigate.setSid(maplist.get(i).get(SQLHelper.SID));
                navigate.setName(maplist.get(i).get(SQLHelper.NAME));
                navigate.setUrl(maplist.get(i).get(SQLHelper.CHNLURL));
                navigate.setPic(maplist.get(i).get(SQLHelper.PIC));
                navigate.setSelected(Integer.valueOf(maplist.get(i).get(SQLHelper.SELECTED)));
                navigate.setType(Integer.valueOf(maplist.get(i).get(SQLHelper.TYPE)));
                navigate.setUad(Integer.valueOf(maplist.get(i).get(SQLHelper.UAD)));
                navigate.setIschange(Integer.valueOf(maplist.get(i).get(SQLHelper.ISCHANGE)));
                navigate.setPushNum(0);
                navigate.setImg(Integer.valueOf(maplist.get(i).get(SQLHelper.IMG)));

                list.add(navigate);
            }
            return list;
        }
        deleteAllChannel();
        if (defaultUserChannels != null) {
            saveUserChannel(defaultUserChannels);
            saveOtherChannel(defaultOtherChannels);
        }
        return defaultUserChannels;
    }

    /**
     * 获取其他的频道
     *
     * @return 数据库存在用户配置 ? 数据库内的其它频道 : 默认其它频道 ;
     */
    public List<ChannelItem> getOtherChannel() {
        Object cacheList = channelDao.listCache(SQLHelper.SELECTED + "= ?", new String[]{"0"});
        List<ChannelItem> list = new ArrayList<ChannelItem>();
        if (cacheList != null && !((List) cacheList).isEmpty()) {
            List<Map<String, String>> maplist = (List) cacheList;
            int count = maplist.size();
            for (int i = 0; i < count; i++) {
                ChannelItem navigate = new ChannelItem();
                navigate.setRid(maplist.get(i).get(SQLHelper.RID));
                navigate.setSid(maplist.get(i).get(SQLHelper.SID));
                navigate.setName(maplist.get(i).get(SQLHelper.NAME));
                navigate.setUrl(maplist.get(i).get(SQLHelper.CHNLURL));
                navigate.setPic(maplist.get(i).get(SQLHelper.PIC));
                navigate.setSelected(Integer.valueOf(maplist.get(i).get(SQLHelper.SELECTED)));
                navigate.setType(Integer.valueOf(maplist.get(i).get(SQLHelper.TYPE)));
                navigate.setUad(Integer.valueOf(maplist.get(i).get(SQLHelper.UAD)));
                navigate.setIschange(Integer.valueOf(maplist.get(i).get(SQLHelper.ISCHANGE)));
                navigate.setPushNum(0);
                navigate.setImg(Integer.valueOf(maplist.get(i).get(SQLHelper.IMG)));
                list.add(navigate);
            }
            return list;
        }
        if (userExist) {
            return list;
        }
        cacheList = defaultOtherChannels;
        return (List<ChannelItem>) cacheList;
    }

    /**
     * 保存用户频道到数据库
     *
     * @param userList
     */
    public void saveUserChannel(List<ChannelItem> userList) {

        for (int i = 0; i < userList.size(); i++) {
            ChannelItem channelItem = (ChannelItem) userList.get(i);
            channelItem.setSelected(Integer.valueOf(1));
            channelDao.addCache(channelItem);
        }
    }

    /**
     * 保存其他频道到数据库
     *
     * @param otherList
     */
    public void saveOtherChannel(List<ChannelItem> otherList) {
        for (int i = 0; i < otherList.size(); i++) {
            ChannelItem channelItem = (ChannelItem) otherList.get(i);
            channelItem.setSelected(Integer.valueOf(0));
            channelDao.addCache(channelItem);
        }
    }

    /**
     * 初始化数据库内的频道数据
     */
//    private void initDefaultChannel() {
//        deleteAllChannel();
//        ChannelItem item = new ChannelItem();
//        defaultUserChannels = item.getDefaultUserChannels();
////        defaultOtherChannels = item.getDefaultOtherChannels();
//        saveUserChannel(defaultUserChannels);
//        saveOtherChannel(defaultOtherChannels);
//    }


}
